<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close">
    <el-row v-if="fromApproval === '1'">
      <el-col :span="24">
        <el-button type="danger" style="float: right" size="small" icon="el-icon-close" circle @click="doCancelEdit"></el-button>
      </el-col>
    </el-row>
    <el-tabs v-model="activeTabName" type="card" @tab-click="tabClick">
      <el-tab-pane label="基础信息" name="basic">
        <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="false">
          <el-row>
            <el-col :span="12">
              <el-form-item label="策划书名称" prop="proposalname" :rules="rules.validateRequire">
                <el-input v-model="model.proposalname"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row>
            <el-col :span="24">
              <el-form-item label="策划书描述" prop="plandescription" :rules="rules.validateRequire">
                <el-input v-model="model.plandescription" type="textarea" :rows="8" maxlength="800" show-word-limit></el-input>
              </el-form-item>
            </el-col>
          </el-row> -->
          <el-row>
            <!-- <el-col :span="12">
              <el-form-item label="策划人工号" prop="plannercode">
                <el-input v-model="model.plannercode"></el-input>
              </el-form-item>
            </el-col> -->
          </el-row>
          <!-- <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称" prop="projectname">
                <el-input v-model="model.projectname"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目编号" prop="projectcode">
                <el-input v-model="model.projectcode"></el-input>
              </el-form-item>
            </el-col>
          </el-row> -->
          <!-- <el-col :span="12">
              <el-form-item label="版本" prop="version">
                <el-input v-model="model.version"></el-input>
              </el-form-item>
            </el-col> -->
          <el-collapse>
            <el-collapse-item title="【单据】申请信息">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="申请部门" prop="applydeptname">
                    <tl-ref-dept v-model="model.applydeptcode" :initDisplayData.sync="model.applydeptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请公司" prop="applyorgname">
                    <el-input v-model="model.applyorgname" :readonly="true" class="is-disabled"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="申请人姓名" prop="applyusername">
                    <tl-ref-user v-model="model.applyusername" :initDisplayData.sync="model.applyusername" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请人工号" prop="applyusercode">
                    <el-input v-model="model.applyusercode" :readonly="true" class="is-disabled"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="单据日期" prop="docdate">
                    <el-date-picker v-model="model.docdate" type="date" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="提交时间" prop="submitdate">
                    <el-date-picker v-model="model.submitdate" type="date" placeholder="提交时间" value-format="yyyy-MM-dd" disabled></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="审批流程" prop="flowid">
                    <el-input v-model="model.dispflowid" :readonly="true" class="is-disabled"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="状态" prop="state">
                    <el-input v-model="model.dispstate" disabled></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-collapse-item>
          </el-collapse>
          <!-- <el-row>
            <el-col :span="12">
              <el-form-item label="申请人姓名2" prop="username">
                <el-input v-model="model.username"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人工号2" prop="usercode">
                <el-input v-model="model.usercode"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="组织" prop="orgname">
                <tl-ref-org v-model="model.orgname" :initDisplayData.sync="model.disporgname" dataType="code" :singleSelect="true"></tl-ref-org>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="组织编码" prop="orgcode">
                <el-input v-model="model.orgcode"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请公司编码" prop="applyorgcode">
                <el-input v-model="model.applyorgcode"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请部门编码" prop="applydeptcode">
                <el-input v-model="model.applydeptcode"></el-input>
              </el-form-item>
            </el-col>
          </el-row> -->
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="里程碑" name="line">
        <tl-line ref="line" :parentId="model.id" :editable="!disableSave" :projectinfo="projectinfo" :planstate="planstate"></tl-line>
      </el-tab-pane>
    </el-tabs>
    <tl-approval-history ref="history" sourceType="GpmProjectPlan" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="false">
        <svg-icon icon-class="save" />保存
      </el-button>
      <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval">
        <svg-icon icon-class="send" />提交审批
      </el-button>
      <tl-attachment sourceType="GpmProjectPlan" :sourceId="model.id" :disabled="disableAttachment" :editable="true"></tl-attachment>
      <el-button type="primary" @click.native="openChangeHistoryDialog('historyList')" v-if="model.id && model.version>1">修改历史</el-button>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
      <!-- <el-button type="primary" @click="doDelApproval" :loading="delApprovalLoading" v-if="!disableEditSubmit" :disabled="disableDelApproval">
        <svg-icon icon-class="undo" />取消审批
      </el-button> -->
    </div>
    <change-log-history ref="historyList" topSourceType="GpmProjectPlan" :topSourceId="model.id + ''"></change-log-history>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
import tlLine from '../gpmProjectMilepost/indexFrame'
import changeLogHistory from "../gpmProject/component/changeLogHistory"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: { tlLine, changeLogHistory },
  data() {
    return {
      moduleTitle: '项目策划',
      urlPrefix: '/sd/projectPlan/',
      url: {
        // 额外的url
      },
      activeTabName: 'basic',
      isRefreshLine: false,
      count: 0,
      planInfo: {},
      fromApproval: ''
    }
  },
  props: {
    projectinfo: {
    },
    oldmilepost: {
      type: String,
      default: ''
    },
    planstate: {
      type: String
    }
  },
  methods: {
    saveInfo(resolve, reject) {
      putAction(this.url.edit, this.model)
        .then(res => {
          if (res.success) {
            this.$emit('refresh')
            resolve()
          } else {
            this.$tap.alert(res.message, 'warning')
            reject()
          }
        })
        .catch(err => {
          this.$tap.alert('修改项目信息失败，请联系系统管理员', 'warning')
          reject()
        })
    },
    assignDefaultValue() {
      if (this.isAdd) {
        this.$set(this.model, "projectid", this.projectinfo.id)
        this.$set(this.model, "deptcode", this.projectinfo.deptcode)
        this.$set(this.model, "deptcommoncode1", this.projectinfo.deptcommoncode1)
        this.$set(this.model, "projectcode", this.projectinfo.projectcode)
        this.$set(this.model, 'applyorgcode', this.org.code)
        this.$set(this.model, 'applyorgname', this.org.name)
        this.$set(this.model, 'applydeptcode', this.dept.code)
        this.$set(this.model, 'applydeptname', this.dept.fullname)
        this.$set(this.model, 'applyusercode', this.user.code)
        this.$set(this.model, 'applyusername', this.user.name)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'docdate', new Date())
        this.$set(this.model, 'deptcode', this.dept.code)
      }
    },
    created() {
    },
    open() {
      this.model = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '新增' + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then(_ => {
              this.assignDefaultValue()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化
            this.$set(this.model, 'enabled', 1)
            this.$set(this.model, 'sortcode', 0)
            this.assignDefaultValue()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        }
      })
    },
    loaded() {
      // 页面加载完毕执行
      this.activeTabName = 'basic'
      this.isRefreshLine = false
      // if (!this.isAdd && this.model.docno) {
      //   this.title = this.title + `（单据编号：${this.model.docno}）`
      // }
    },
      afterSubmit() {
      if (this.oldmilepost != null) {
        var datamap = {
          oldmilepost: this.oldmilepost,
          projectname: this.projectinfo.projectname
        }
        postAction('sd/projectMilepost/insertBatch', datamap).then(res => {
          if (res.success) {
          } else {
            this.$tap.alert(res.message, "warning")
          }
        })
      }
    },
    tabClick(tab) {
      if (tab.name === 'line') {
        if (!this.model.id) {
          this.$nextTick((_) => {
            this.activeTabName = 'basic'
          })
          this.$tap
            .confirm('当前数据未保存，是否立即保存数据？')
            .then((_) => {
              this.doSubmitEdit()
            })
            .catch((_) => { })
        } else if (!this.isRefreshLine) {
          this.$refs.line.doQuery()
          this.isRefreshLine = true
        }
        this.$refs.line.initPage()
      }
    },
    doCancelEdit() {
      this.visible = false
      this.$emit('close')
      this.$emit('refresh')
    },
    openChangeHistoryDialog(historyList) {
      /** 打开详情对话框实现*/
      this.$refs[historyList].visible = true
    }
  }
}
</script>

